<?php


namespace App\HttpController;


use App\Common\lib\Code;
use App\Exception\ApiException;
use App\Logic\UserLogic;
use App\Models\UserModel;
use EasySwoole\EasySwoole\Logger;

class UserInfo extends Base
{

    /**
     * 获取个人信息
     */
    public function info()
    {
        return $this->writeJson(Code::$success, $this->getUser(), 'ok');
    }

    /**
     * 用户编辑
     * @throws ApiException
     * @throws \Throwable
     */
    public function user_edit(): bool
    {
        try {
            return $this->writeJson(Code::$success,(new UserLogic())->user_edit($this->request()->getRequestParam(),$this->getUser()),'ok');
        } catch (\Exception $exception) {
            if ($exception instanceof ApiException){
                throw new ApiException($exception->getMessage());
            }else{
                Logger::getInstance()->error($exception->getMessage());
            }
        }
    }

    /**
     * 上传图片
     */
    public function file(){
        try {
            $request = $this->request();
            $file = $request->getUploadedFile('file');
            $fileType = $file->getClientMediaType();
            //检测大小
            if (($file->getSize() / 1024)  > 1024) {
                return $this->writeJson(Code::$error,null,"图片最大1M");
            }
            $rand_str = uniqid(microtime(true));
            $file->moveTo('/img/'.$rand_str.'.png'); // 失败这里会抛出异常 \EasySwoole\Http\Exception\FileException
        }catch (\Exception $exception){
            return $this->writeJson(Code::$error,null,"error");
        }
        return $this->writeJson(Code::$success,['img'=>$rand_str.'.png'],'ok');
    }

}